define(['./../initial', './../service/chartsFactory', './../service/toggleSideBar', './../service/viewMode', './../service/loadProjectMap', './../service/webSocket/webSocketEmitter'], function() {
    return (function() {
        var app = angular.module('App.home');
        app.directive('vtabs', ['$timeout', function($timeout) {
            return {
                restrict: 'E',
                transclude: true,
                scope: {
                    curPrj: '=',

                },
                controller: ['$scope', '$element',
                    function($scope, $element) {
                        
                        $('#leftBar').scrollTop(0);

                        $scope.toggleAccordion = function(event, type) {
                            var current = event.currentTarget;
                            var triangle = angular.element(current.querySelector('.triangle'));
                            var target = event.currentTarget.parentElement.querySelector('.content.menu');
                            var target1 = angular.element(target);
                            $('#projectPane .content.menu').removeClass('select');
                            target1.addClass('select');
                            $('#projectPane .content.menu.show').not(".select").removeClass('show').addClass('hide');
                            $timeout(function() {
                                if (!target1.hasClass('show')) {
                                    target1.removeClass('hide').addClass('show select');
                                    // selectFirstAccordion(type);
                                } else {
                                    target1.removeClass('show').addClass('hide select');
                                }
                            }, 500);
                        };


                        function selectFirstAccordion(type) {
                            var targetIndex = 0;
                            for (var i = 0; i < $scope.curPrj.monitorPoints.length; i++) {
                                if ($scope.curPrj.monitorPoints[i].monitorType === type) {
                                    break;
                                } else {
                                    targetIndex += $scope.curPrj.monitorPoints[i].locationPoints.length;
                                }
                            }
                            $scope.panes[targetIndex].select();
                        }


                        var panes = this.panes = $scope.panes = [];



                        var fnc = this.select = function(pane) {
                            angular.forEach(panes, function(p) {
                                if (p !== pane) {
                                    p.selected = false;
                                    p.expanded = false;
                                } else {
                                    pane.selected = true;
                                }
                            });
                        };

                        this.addPane = function(pane) {
                            panes.push(pane);
                            if (panes.length === 1) {
                                pane.select();
                            }
                            pane.index = panes.length - 1;
                        };

                        this.removePane = function(pane) {
                            //var panes = this.panes = $scope.panes = [];
                            for (var i = 0; i < panes.length; i++) {
                                if (pane === panes[i]) {
                                    panes.splice(i, 1);
                                }
                            }
                        };
                    }
                ],
                templateUrl: 'partials/vtabsTemplate.html',
                replace: true
            };
        }]);

        app.directive('vpane', ['chartsFactory', 'toggleSideBar', 'viewMode', 'mapInstance', '$timeout', '$q', '$rootScope', 'webSocketEmitter', 'loadProjectMap',
            function(chartsFactory, toggleSideBar, viewMode, mapInstance, $timeout, $q, $rootScope, webSocketEmitter, loadProjectMap) {
                return {
                    require: '^vtabs',
                    restrict: 'E',
                    transclude: true,
                    scope: {
                        header: '=',
                        count: '=',
                        prjid: '=',
                        mtype: '=',
                        location: '=',
                        isAutoSurveyPrj: '=',
                        autoSurveyPrjOnline: '=',
                        autoSurveyPrjName: "=",
                        isRingConfig: "="
                    },
                    link: function(scope, element, attrs, vtabsCtrl) {
                        var toggleLeftBar = toggleSideBar.toggleLeftSideBar;
                        var toggleBottomBar = toggleSideBar.toggleBottomSideBar;



                        scope.select = function(autoSurvey) {
                            //autoSurvey 是否由自动化测量发起的该事件,如果'是',曲线最近变化曲线和地图点加载就不需要了
                            var defer = $q.defer();
                            if (scope.selected === true) {
                                defer.resolve();
                            } else {
                                vtabsCtrl.select(scope);
                                var prj = $rootScope.curPrj;
                                $rootScope.$broadcast('selectChartPane'); //选择到chart,发送到tabs.js
                                toggleLeftBar($rootScope.smallScreen ? 'hide' : 'show')
                                    .then(function() {
                                        if (!autoSurvey) {
                                            return $q.all([prj.getRecentChanges(scope.mtype, scope.location), toggleBottomBar('show'), prj.getMainStatusTimeList()]);
                                        }
                                    })
                                    .then(function(qAllDatas) {
                                        if (!autoSurvey) {
                                            var recentChanges = qAllDatas[0];
                                            //设置locationpoints的在选时间列表
                                            prj.findLocationPoints(scope.mtype, scope.location).selectedDates = [recentChanges.lastTime, recentChanges.curTime];
                                            //load calendar
                                            var date = new Date(recentChanges.curTime);
                                            $("#calendar-pane").calendarWidget({
                                                month: date.getMonth(),
                                                year: date.getFullYear(),
                                                validTimeList: recentChanges.allTimes,
                                                statusTimeList: prj.mainStatusTimeList,
                                                onClick: (function(monitorType, location) {
                                                    return function(event) {
                                                        toggleBottomBar('hide')
                                                            .then(function() {
                                                                prj.findLocationPoints(monitorType, location).selectedDates = event.clickDates;
                                                                return prj.getRecent2Changes(monitorType, location, event.clickDates);
                                                            })
                                                            .then(function(data) {
                                                                $rootScope.$broadcast('selectChartPane'); //选择到chart;
                                                                //一系列的变化曲线
                                                                return chartsFactory.createPointsChartListByLocation('#chart', data);
                                                            })
                                                            .then(function() {
                                                                return toggleBottomBar('show');
                                                            });
                                                    };
                                                })(scope.mtype, scope.location)
                                            });
                                            return chartsFactory.createPointsChartByLocation('#chart', recentChanges);
                                        }
                                    }).then(function() {
                                        viewMode.mode = 'multiple';
                                        viewMode.monitorType = scope.mtype;
                                        viewMode.location = scope.location;
                                        viewMode.pointName = '';
                                        var map = mapInstance.getMap();
                                        if (map.viewState === 'active' && !autoSurvey) {
                                            map.fireEvent('renderPointsRequired');
                                        }
                                        defer.resolve();
                                    });
                            }
                            return defer.promise;
                        };

                        scope.startAutoSurveyPrj = function() {
                            if (!scope.autoSurveyPrjOnline) return;
                            var autoSurveyPrjName = scope.autoSurveyPrjName;
                            var prj = $rootScope.curPrj;
                            toggleLeftBar('show').then(function() {
                                return scope.select(true); //true表示由自动化发起的
                            }).then(function() {
                                return loadProjectMap(prj, {
                                    autoSurvey: true,
                                    autoSurveyPrjName: autoSurveyPrjName
                                });
                            }).then(function() {
                                webSocketEmitter.SelectProject(autoSurveyPrjName); //选择该工程
                                webSocketEmitter.StartSurvey(autoSurveyPrjName); //发送测量命令
                            });
                        };

                        element.bind("$destroy", function() {
                            vtabsCtrl.removePane(scope);
                        });
                        vtabsCtrl.addPane(scope);
                    },
                    templateUrl: 'partials/vpaneTemplate.html',
                    replace: true
                };
            }
        ]);
        return true;
    })();
});
